import os
from datetime import datetime

from sunshinelife import settings

# kindeditor  图片上传指定的返回参数   1 错误   2 正确
def saveImg(img):
    type = img.name.split('.')[-1]
    if type not in settings.MEDIA_ALLOWED_EXTENSIONS:
        return {'error': 1, "message": "类型不符"}
    if img.size > settings.MEDIA_MAX_SIZE:
        return {'error': 1, "message": "图片过大"}

    # 获取当前时间
    now = datetime.now()
    # 格式化日期为YYYYMMDD格式
    formatted_date = now.strftime('%Y%m%d%H%M%S')

    img.name = formatted_date + "." + type
    path_file = os.path.join(settings.MEDIA_ROOT, img.name)
    open(path_file, 'wb').write(img.file.read())
    return {'error': 0 , "url": "/static/media/%s" % img.name}
